package com.dqhl.community.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.adapter.HealthyFilelistAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.HealthyFile;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2016/9/7.
 * 健康档案 activity
 */
public class HealthyFileActivity extends BaseActivity implements View.OnClickListener {

    private final static int CREATE_FILE = 100;
    private ImageView ivTopBack;
    private TextView tvTopTitle;
    private TextView tvTopOperation;

    private ListView lvHealthyFile;
    private HealthyFilelistAdapter healthyFilelistAdapter;
    private List<HealthyFile> healthyFileList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_healthy_file);

        initView();
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        showCustomLoadBar("加载中……");
        RequestParams params = new RequestParams(Config.Api.get_healthy_file);
        params.addBodyParameter("user_id", app.getUserId());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    String record = JsonUtils.getInfo(data, "data");
                    Dlog.e(record);
                    healthyFileList = parseData(record);
                    Dlog.e("healthyFileList:" + healthyFileList);
                    healthyFilelistAdapter = new HealthyFilelistAdapter(HealthyFileActivity.this,
                            healthyFileList);
                    lvHealthyFile.setAdapter(healthyFilelistAdapter);
                    healthyFilelistAdapter.setOnPictureClickListener(onPictureClickListener);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });

    }

    HealthyFilelistAdapter.OnPictureClickListener onPictureClickListener = new
            HealthyFilelistAdapter.OnPictureClickListener() {
        @Override
        public void showBigPicture(String picPath) {
            Bundle bundle = new Bundle();
            bundle.putString("img_path", picPath);
            overlay(ImageShowerActivity.class, bundle);
        }
    };

    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText(R.string.txt_healthy_file);
        tvTopOperation = (TextView) findViewById(R.id.tv_top_operation);
        tvTopOperation.setText("新建");

        lvHealthyFile = (ListView) findViewById(R.id.lv_healthy_file);

        ivTopBack.setOnClickListener(this);
        tvTopOperation.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_top_operation: //新建健康档案
                overlay(CreateHealthyFileActivity.class, CREATE_FILE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CREATE_FILE && resultCode == RESULT_OK) {
            // 刷新列表
            Dlog.e("刷新列表");
            healthyFilelistAdapter.notifyDataSetChanged();
            getData();
        }
    }

    /**
     * 解析档案列表
     *
     * @param str
     * @return
     */
    private List<HealthyFile> parseData(String str) {
        List<HealthyFile> data = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(str);
            HealthyFile healthyFile;
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.getJSONObject(i);
                healthyFile = new HealthyFile();
                healthyFile.setId(object.getString("id"));
                healthyFile.setUser_id(object.getString("user_id"));
                healthyFile.setCreate_time(object.getString("create_time"));
                healthyFile.setInfo(object.getString("info"));
                healthyFile.setPicsum(object.getInt("picsum"));
                healthyFile.setPic_a(object.getString("pic_a"));
                healthyFile.setPic_b(object.getString("pic_b"));
                healthyFile.setPic_c(object.getString("pic_c"));
                healthyFile.setPic_d(object.getString("pic_d"));
                healthyFile.setPic_e(object.getString("pic_e"));
                healthyFile.setPic_f(object.getString("pic_f"));
                data.add(healthyFile);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return data;
    }
}
